package com.android.personal.data;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class FinanceDataAdaptater {

	private static SQLiteDatabase mdb;
	private static FinanceDatabaseHelper mDbHelper;
	private static Context mCtx;

	private static final int DATABASE_VERSION = 3;

	public static void CreateFinanceDataAdaptater(Context ctx) {

		mCtx = ctx;
	}

	public static void open() throws SQLException {

		mDbHelper = new FinanceDatabaseHelper(mCtx,
				FinanceDataTags.DATABASE_NAME, null, DATABASE_VERSION);
		mdb = mDbHelper.getWritableDatabase();
	}

	public static void close() {
		mDbHelper.close();
	}
	
	/*************************************************/

	/**
	 * Accounts DB Requests functions.
	 * 
	 */

	public static Cursor fetchAllAccounts() {

		return mdb
				.query(FinanceDataTags.TABLE_ACCOUNTS, new String[] {
						FinanceDataTags.ACCOUNT_KEY,
						FinanceDataTags.ACCOUNT_NAME,
						FinanceDataTags.ACCOUNT_BALANCE }, null, null, null,
						null, null);
	}
	
	public static Cursor fetchAccounts() {

		return mdb
				.query(FinanceDataTags.TABLE_ACCOUNTS, new String[] {
						FinanceDataTags.ACCOUNT_KEY,
						FinanceDataTags.ACCOUNT_NAME}, 
						null, null, null, null, null);
	}
	
	public static long createAccount(String title, String balance) {

		ContentValues initialValues = new ContentValues();
		initialValues.put(FinanceDataTags.ACCOUNT_NAME, title);
		initialValues.put(FinanceDataTags.ACCOUNT_BALANCE, balance);

		return mdb.insert(FinanceDataTags.TABLE_ACCOUNTS, null,
				initialValues);
	}

	public static boolean updateAccount(String rowId, String name,
			String balance) {

		ContentValues args = new ContentValues();
		args.put(FinanceDataTags.ACCOUNT_NAME, name);
		args.put(FinanceDataTags.ACCOUNT_BALANCE, balance);

		return mdb.update(FinanceDataTags.TABLE_ACCOUNTS, args,
				FinanceDataTags.ACCOUNT_KEY + "=" + rowId, null) > 0;
	}

	public static boolean deleteAccount(long rowId) {

		return mdb.delete(FinanceDataTags.TABLE_ACCOUNTS,
				FinanceDataTags.ACCOUNT_KEY + "=" + rowId, null) > 0;
	}

	/*************************************************/

	/**
	 * Operations DB Requests functions.
	 * 
	 */

	public static Cursor fetchAllOperations() {
		
		String query = "select " + FinanceDataTags.TABLE_OPERATIONS
		+ "." + FinanceDataTags.OPERATION_KEY + " as " 
		+ FinanceDataTags.OPERATION_KEY
		+ ", " + FinanceDataTags.OPERATION_VALUE
		+ ", " + FinanceDataTags.OPERATION_DATE
		+ ", " + FinanceDataTags.OPERATION_VALUE
		+ ", " + FinanceDataTags.OPERATION_CATEGORY
		+ ", " + FinanceDataTags.ACCOUNT_NAME
		+ " from " + FinanceDataTags.TABLE_OPERATIONS 
		+ ", " + FinanceDataTags.TABLE_ACCOUNTS
		+ " where " + FinanceDataTags.TABLE_ACCOUNTS 
		+ "." + FinanceDataTags.ACCOUNT_KEY + " = "
		+ FinanceDataTags.OPERATION_ACCOUNT_ID; 
		
		return mdb.rawQuery(query,null);
	}

	public static long createOperation(String title, String value, String date,
			String category) {

		ContentValues initialValues = new ContentValues();
		initialValues.put(FinanceDataTags.OPERATION_ACCOUNT_ID, title);
		initialValues.put(FinanceDataTags.OPERATION_VALUE, value);
		initialValues.put(FinanceDataTags.OPERATION_DATE, date);
		initialValues.put(FinanceDataTags.OPERATION_CATEGORY, category);

		return mdb.insert(FinanceDataTags.TABLE_OPERATIONS, null,
				initialValues);
	}

	public static boolean deleteOperation(long rowId) {

		return mdb.delete(FinanceDataTags.TABLE_OPERATIONS,
				FinanceDataTags.OPERATION_KEY + "=" + rowId, null) > 0;
	}
	
	/*************************************************/

}
